<?php

namespace app\common\library;

use alipay\aop\AopClient;
use alipay\aop\request\AlipayTradeAppPayRequest;
use app\common\controller\Api;
use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\Signer;
use app\common\model\MoneyLog;
use app\common\model\order\Order;
use app\common\model\User;
use app\common\model\UserGroup;
use EasyWeChatComposer\EasyWeChat;
use think\Db;
use think\Env;
use think\Log;
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Formatter;
use WeChatPay\Util\PemUtil;
use wechatPay\Wxpay;

/**
 * 统一支付接口
 */
class Pay extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function __construct()
    {
    }
    /**
     * 余额支付
     *
     */
    public static function yue($data, $payPwd)
    {
        $user = User::get($data['user_id']);

        if ($user->pay_pwd != md5(md5($payPwd) . 'pcy')) {
            exception('支付密码不正确');
        }

        $totalMoney = bcadd($user->money, $user->no_money, 2);
        if ($data['pay_money'] > $totalMoney) {
            exception('余额不足');
        }
        Db::startTrans();
        try {
            if ($user->no_money > $data['pay_money']) {
                MoneyLog::income($user->id, $data['pay_money'], 1, 503, $data['sn']);
            } else {
                if ($user->no_money > 0) {
                    $money = bcsub($data['pay_money'], $user->no_money, 2);
                    MoneyLog::income($user->id, $user->no_money, 1, 503, $data['sn']);
                } else {
                    $money = $data['pay_money'];
                }
                if ($money > 0) {
                    MoneyLog::income($user->id, $money, 1, 504, $data['sn']);
                }
            }

            self::orderFulfillment($data['sn']);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }
    /**
     * 支付宝支付
     *
     */
    public static function alipay($data)
    {
        if (Env::get('app.debug') == true) {
            $data['pay_money'] = 0.01;
        }
        $aop = new AopClient();
        $object['out_trade_no'] = $data['sn'];
        $object['total_amount'] = $data['pay_money'];
        $object['subject'] = '订单支付';
        $json = json_encode($object);
        $request = new AlipayTradeAppPayRequest();
        $request->setNotifyUrl(cdnurl('/api/notify/alipayNotify', true));
        $request->setBizContent($json);
        $result = $aop->sdkExecute($request);
        return $result;
    }
    //小程序下单
    public static function minipay($order, $openid, $attach = 'order')
    {
        $out_trade_no = $order['sn'];
        $money = $order['pay_money'];
        if (Env::get('app.debug') == true) {
            $money = 0.01;
        }
        if (empty($out_trade_no)) {
            exception('缺少参数order_sn');
        }
        // 官方提供网址
        $url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
        $urlarr = parse_url($url); //拆解为：[scheme=>https,host=>api.mch.weixin.qq.com,path=>/v3/pay/transactions/native]
        $time = time(); //时间戳
        $noncestr = Wxpay::getNonceStr();
        $appid = 'wx7278ddfbcda36fb4'; //appID
        $mchid = config('site.mch_id'); //商户ID
        $xlid =  config('site.serial_no'); //证书序列号
        $data = array();
        $data['appid'] = $appid;
        $data['mchid'] = $mchid;
        $data['attach'] = $attach;
        $data['description'] = '商品描述'; //商品描述
        $data['out_trade_no'] = $out_trade_no; //订单编号，订单号在微信支付里是唯一的
        $data['notify_url'] =  cdnurl('/api/notify/wechatNotify', true); //需根据自己的情况修改回调接口，也可以为空
        $data['amount']['total'] = $money * 100; //金额 单位 分
        $data['scene_info']['payer_client_ip'] = $_SERVER["REMOTE_ADDR"];; //场景ip
        $data['payer']['openid'] = $openid; //openid
        $data = json_encode($data);
        Log::write($data);

        //签名，包含了$data数据、微信指定地址、随机数和时间
        $key = Wxpay::getSign($data, $urlarr['path'], $noncestr, $time);
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $mchid, $xlid, $noncestr, $time, $key);
        //头部信息
        $header  = array(
            'Accept: application/json',
            'Content-Type: application/json',
            'User-Agent:*/*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token
        );
        //向微信接口地址提交json格式的$data和header的头部信息，得到返回值
        $res = Wxpay::curl_post_https($url, $data, $header);
        $prepay_id = json_decode($res, true)['prepay_id'];
        $paySign = Wxpay::getWechartSign($appid, $time, $noncestr, 'prepay_id=' . $prepay_id);
        $payData = [
            'timeStamp' => (string) $time,
            'nonceStr' => $noncestr,
            'package' => 'prepay_id=' . $prepay_id,
            'signType' => 'RSA',
            'paySign' => $paySign
        ];
        return $payData;
    }
    // 0 1 * * *  /usr/bin/php /www/wwwroot/1214test.ciyuantiaoyue.com/1214-3.0/public/index.php /addons/crontab/autotask/index
    //微信app支付
    public static function wxpay($order,  $attach)
    {
        $out_trade_no = $order['sn'];
        $money = $order['pay_money'];
        if (Env::get('app.debug') == true) {
            $money = 0.01;
        }
        if (empty($out_trade_no)) {
            exception('缺少参数order_sn');
        }
        // 官方提供网址
        $url = "https://api.mch.weixin.qq.com/v3/pay/transactions/app";
        $urlarr = parse_url($url); //拆解为：[scheme=>https,host=>api.mch.weixin.qq.com,path=>/v3/pay/transactions/native]
        $time = time(); //时间戳
        $noncestr = Wxpay::getNonceStr();
        $appid = config('site.app_id'); //appID
        $mchid = config('site.mch_id'); //商户ID
        $xlid =  config('site.serial_no'); //证书序列号
        $data = array();
        $data['appid'] = $appid;
        $data['mchid'] = $mchid;
        $data['attach'] = $attach;
        $data['description'] = '商品描述'; //商品描述
        $data['out_trade_no'] = $out_trade_no; //订单编号，订单号在微信支付里是唯一的
        $data['notify_url'] =  cdnurl('/api/notify/wechatNotify', true); //需根据自己的情况修改回调接口，也可以为空
        $data['amount']['total'] = $money * 100; //金额 单位 分
        $data['scene_info']['payer_client_ip'] = $_SERVER["REMOTE_ADDR"];; //场景ip
        // $data['payer']['openid'] = $openid; //app支付不需要openid
        $data = json_encode($data);
        Log::write($data);

        //签名，包含了$data数据、微信指定地址、随机数和时间
        $key = Wxpay::getSign($data, $urlarr['path'], $noncestr, $time);
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $mchid, $xlid, $noncestr, $time, $key);
        //头部信息
        $header  = array(
            'Accept: application/json',
            'Content-Type: application/json',
            'User-Agent:*/*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token
        );
        //向微信接口地址提交json格式的$data和header的头部信息，得到返回值
        $res = Wxpay::curl_post_https($url, $data, $header);
        Log::write($res);
        $prepay_id = json_decode($res, true)['prepay_id'];


        $paySign = Wxpay::getWechartSign($appid, $time, $noncestr, $prepay_id);
        $payData = [
            'appid' => $appid,
            'partnerid' => $mchid,
            'prepayid' => $prepay_id,
            'package' => "Sign=WXPay",
            'noncestr' => $noncestr,
            'timestamp' => (string) $time,
            'sign' => $paySign
        ];
        return $payData;
    }

    //订单支付成功
    public static function orderFulfillment($sn, $trade_no = null)
    {
        $order = Order::get([
            'status' => 0,
            'sn' => $sn,
        ], 'goods');
        if (!$order) {
            exception('订单不存在');
        }
        $user = User::get($order->user_id);
        $data['status'] = 1;
        if ($order->distribution == '2') {
            $data['status'] = 2;
            $data['qh_image'] = build($order->id);
        }
        $order['transaction_id'] = $trade_no;
        $data['paytime'] = time();
        $order->save($data);
        $user->setInc('consumption', $order->pay_money);
        // $money = $order->pay_money - $order->freight;
        // //订单分佣
        // MoneyLog::subcommission($money, $user->id, $sn);
        //权益值
        // if ($order->p_id) {
        //     $puser = User::get($order->p_id);
        //     $puser->setInc('total_interests', $order->p_interests);
        // }
        // if ($order->dividend > 0) {
        //     $user->setInc('use_dividend', $order->dividend);
        // }
        // if ($order->interests > 0) {
        //     $user->setInc('total_interests', $order->interests);
        // }
           if ($order->group_id >0) {
                $user->old_grade = $user->group_id;
                if ($order->group_id > $user->group_id) {
                    $user->group_id = $order->group_id;
                }
                $user->save();
           }
      
        //根据消费额度升级代理等级
        // User::nextGroup($order, $user->id);
        //升级星级用户
        // if ($user->paren_ids) {
        //     User::starUpgrade($user->paren_ids);
        // }
    }
}
